//
//  PostFileViewController.m
//  云律司
//
//  Created by 韦淋 on 2017/6/26.
//  Copyright © 2017年 test. All rights reserved.
//

#import "PostFileViewController.h"


@interface PostFileViewController ()<HXPhotoViewDelegate>
@property (strong, nonatomic) HXPhotoManager *manager;

@end

@implementation PostFileViewController

- (HXPhotoManager *)manager
{
    if (!_manager) {
        _manager = [[HXPhotoManager alloc] initWithType:HXPhotoManagerSelectedTypePhotoAndVideo];
    }
    return _manager;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Do any additional setup after loading the view from its nib.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.translucent = NO;
    self.automaticallyAdjustsScrollViewInsets = YES;
    CGFloat width = self.view.frame.size.width;
    HXPhotoView *photoView = [[HXPhotoView alloc] initWithFrame:CGRectMake(12, 100, width - 24, 0) WithManager:self.manager];
    photoView.delegate = self;
    photoView.backgroundColor = [UIColor whiteColor];
    
    UIButton *submit = [[UIButton alloc] initWithFrame:CGRectMake(100, 500, 200, 50)];
    submit.backgroundColor = [UIColor redColor];
    [submit addTarget:self action:@selector(submit) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:submit];
    [self.view addSubview:photoView];
}
- (void)submit{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",
                                                         
                                                         @"text/html",
                                                         
                                                         @"image/jpeg",
                                                         
                                                         @"image/png",
                                                         
                                                         @"application/octet-stream",
                                                         
                                                         @"text/json",
                                                         
                                                         nil];
    
    manager.requestSerializer= [AFHTTPRequestSerializer serializer];
    
    manager.responseSerializer= [AFHTTPResponseSerializer serializer];
    
    [manager POST:SUBMITFILE_URL parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        
        ;
        
        if (self.manager.selectedPhotos.count == 0 && self.manager.selectedVideos.count == 0) {
            [MBProgressHUD showSuccess:@"请添加文件"];
            return ;
        }
        
        UIImage *image = [self.manager.selectedPhotos firstObject];
            
        NSData *data = UIImagePNGRepresentation(image);
            
        //上传的参数(上传图片，以文件流的格式)
            
        [formData appendPartWithFileData:data name:@"file" fileName:@"gauge.png" mimeType:@"image/png"];
        
        
        
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        
        
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSLog(@"上传成功");
        
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        NSLog(@"上传失败%@",error);
        
        
        
    }];
    
}
- (void)photoViewChangeComplete:(NSArray *)allList Photos:(NSArray *)photos Videos:(NSArray *)videos Original:(BOOL)isOriginal
{
    
    
    NSLog(@"%ld - %ld - %ld",allList.count,photos.count,videos.count);
    NSLog(@"%@",allList);
}

- (void)photoViewUpdateFrame:(CGRect)frame WithView:(UIView *)view
{
    NSLog(@"%@",NSStringFromCGRect(frame));
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
